package ruifeng.zcj.dig.ui.forum.base;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;

import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewHolder;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.base.api.OSChinaApi;
import ruifeng.zcj.dig.groble.FragmentArgs;
import ruifeng.zcj.dig.groble.ToActivityUtil;
import ruifeng.zcj.dig.ui.base.RefreshRecyclerFragment;
import ruifeng.zcj.dig.ui.forum.ForumDetailActivity;
import ruifeng.zcj.dig.ui.forum.bean.NewPost;
import ruifeng.zcj.mybase.base.utils2.AbJsonUtil;

/**
 * Created by Administrator on 2016/10/24 0024.
 */
public class BasePostFragment extends RefreshRecyclerFragment {

    protected NewPost mNewPost;


    @Override
    protected void initFragmentConfig(View view) {
        super.initFragmentConfig(view);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    }

    @Override
    protected BGARecyclerViewAdapter getAdapter() {
        return new MomentAdapter2(recyclerView);
    }

    @Override
    protected void onFirstUserVisible() {
        /*OSChinaApi.getpostingslist("0", null, null, "1", "10", new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (!TextUtils.isEmpty(responseInfo.result)) {
                    mNewPost = (NewPost) AbJsonUtil.fromJson(responseInfo.result, NewPost.class);
                    mMomentAdapter.addNewData(mNewPost.getRows());
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                showSnackBar("日志:" + s);
            }

        });*/
    }

    public class MomentAdapter2 extends BGARecyclerViewAdapter<NewPost.RowsBean> {

        public MomentAdapter2(RecyclerView recyclerView) {
            super(recyclerView, R.layout.listview_item_forum);
        }

        @Override
        public void fillData(BGAViewHolderHelper helper, int position, NewPost.RowsBean moment) {
            /*if (TextUtils.isEmpty(moment.content)) {
                helper.setVisibility(R.id.tv_item_moment_content, View.GONE);
            } else {
                helper.setVisibility(R.id.tv_item_moment_content, View.VISIBLE);
                helper.setText(R.id.tv_item_moment_content, moment.content);
            }*/
            helper.setText(R.id.forum_fragment_item_text_title, moment.getPostingsname());// 标题
            helper.setText(R.id.forum_fragment_item_text_sun_title, moment.getUsername());// 名称
            helper.setText(R.id.forum_fragment_item_text_time, moment.getTime());// 时间
            helper.setText(R.id.views, moment.getViews() + "次浏览");// 浏览次数*/


          /*
            /*TextView postingsname = helper.getView(R.id.forum_fragment_item_text_title);

            BGANinePhotoLayout ninePhotoLayout = helper.getView(R.id.npl_item_moment_photos);

            ninePhotoLayout.init(MomentListActivity.this);
            ninePhotoLayout.setDelegate(MomentListActivity.this);
            ninePhotoLayout.setData(moment.photos);*/
        }

        @Override
        public void onViewRecycled(BGARecyclerViewHolder holder) {
            super.onViewRecycled(holder);

           /* BGANinePhotoLayout ninePhotoLayout = holder.getViewHolderHelper().getView(R.id.npl_item_moment_photos);
            ninePhotoLayout.init(null);
            ninePhotoLayout.setDelegate(null);*/
        }
    }


    @Override
    protected void onUserVisible() {

    }

    @Override
    protected void onUserInvisible() {

    }

    @Override
    protected void cleanMemory() {

    }


    @Override
    public void onRVItemClick(ViewGroup viewGroup, View view, int position) {
        NewPost.RowsBean rowsBean = mNewPost.getRows().get(position);
        FragmentArgs fragmentArgs2 = new FragmentArgs();
        fragmentArgs2.add("newpost", rowsBean);
        ToActivityUtil.toNextActivity(getContext(), ForumDetailActivity.class, "newpostBundle", fragmentArgs2);
    }

    @Override
    public boolean onRVItemLongClick(ViewGroup viewGroup, View view, int position) {
        showSnackBar("长按了item " + position);
        return true;
    }

}
